package com.yanggu.payment.controller;

import com.alipay.api.AlipayApiException;
import com.yanggu.payment.service.AliPayService;
import com.yanggu.payment.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2022/5/10 15:19
 * @description:
 */
@RestController
@Api(tags = "网站支付宝支付API")
@RequestMapping("/api/ali-pay")
public class ALiPayController {

    @Autowired
    private AliPayService aliPayService;

    /**
     * 统一收单下单并支付页面接口
     * 通过商品id进行下单
     * 支付宝下单接口返回前端表单页面
     * https://opendocs.alipay.com/open/028r8t?scene=22
     */
    @PostMapping("/trade/page/pay/{productId}")
    @ApiOperation("统一收单下单并支付页面接口")
    public R tradePagePay(@PathVariable("productId") Long productId) throws AlipayApiException {
        String formStr = aliPayService.tradePagePay(productId);
        return R.ok().data("formStr", formStr);
    }

    /**
     * 统一收单线下交易查询
     * https://opendocs.alipay.com/open/028woa
     *
     * @param orderNo
     * @return
     * @throws AlipayApiException
     */
    @ApiOperation("统一收单线下交易查询")
    @GetMapping("/trade/query/{orderNo}")
    public R tradeQuery(@PathVariable("orderNo") String orderNo) throws AlipayApiException {
        String data = aliPayService.tradeQuery(orderNo);
        return R.ok().data("data", data);
    }

    /**
     * 支付宝支付成功, 异步回调函数
     * https://opendocs.alipay.com/open/270/105902
     */
    @PostMapping("/trade/notify")
    @ApiOperation("支付宝支付成功, 异步回调函数")
    public String tradeNotify(@RequestParam Map<String, String> paramsMap) throws AlipayApiException {
        return aliPayService.tradeNotify(paramsMap);
    }

    /**
     * 统一收单交易关闭接口
     * https://opendocs.alipay.com/open/028wob
     */
    @PostMapping("/trade/close/{orderNo}")
    @ApiOperation("统一收单交易关闭接口")
    public R tradeClose(@PathVariable("orderNo") String orderNo) throws AlipayApiException {
        aliPayService.tradeClose(orderNo);
        return R.ok().setMessage("订单已经成功取消");
    }

    /**
     * 申请退款 支付宝退款是同步返回(除了银行卡退款外)
     * https://opendocs.alipay.com/open/028sm9
     * @param orderNo
     * @param reason
     * @return
     */
    @PostMapping("/trade/refund/{orderNo}/{reason}")
    @ApiOperation("申请退款")
    public R refunds(@PathVariable("orderNo") String orderNo,
                     @PathVariable("reason") String reason) throws AlipayApiException {
        aliPayService.refunds(orderNo, reason);
        return R.ok();
    }

    /**
     * 统一收单交易退款查询
     * https://opendocs.alipay.com/open/028sma
     */
    @ApiOperation("统一收单交易退款查询")
    @GetMapping("/trade/fastpay/refund/query/{orderNo}")
    public R refundQuery(@PathVariable("orderNo") String orderNo) throws AlipayApiException {
        String result = aliPayService.refundQuery(orderNo);
        return R.ok().data("result", result);
    }

    @ApiOperation("下载交易订单或者资金订单")
    @GetMapping("/bill/downloadurl/query/{billDate}/{type}")
    public R downloadBill(@PathVariable("billDate") String billDate,
                          @PathVariable("type") String type) throws AlipayApiException {
        String result = aliPayService.downloadBill(billDate, type);
        return R.ok().data("downloadUrl", result);
    }

}
